package com.aj.service;

import com.aj.Interface.SetmealService;
import com.aj.common.RedisConstant;
import com.aj.dao.SetmealDao;
import com.aj.pojo.CheckItem;
import com.aj.pojo.Setmeal;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import entity.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void add(Setmeal setmeal,Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        if (checkgroupIds!=null&&checkgroupIds.length>0){
            setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);
        }
        savePic2Redis(setmeal.getImg());
    }


    /**
     * 分页查询
     */
    @Override
    public PageResult findpage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setmealDao.findpage(queryPageBean.getQueryString());
        PageResult result =  new PageResult(page.getTotal(),page.getResult());
        return result;
    }



    private void savePic2Redis(String img) {
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
    }


    private void setSetmealAndCheckGroup(Integer setmealid, Integer[] checkgroupIds) {

        for (Integer checkgroupId : checkgroupIds) {
            Map map = new HashMap();
            map.put("setmealid", setmealid);
            map.put("checkgroupId",checkgroupId );
            setmealDao.setSetmealAndCheckGroup(map);
        }

    }

    /**
     * 查询所有的套餐
     * @return
     */
    @Override
    public JSONArray findAll() {
        JSONArray objects = null;
        String setmealliststring=null;
        try {
             setmealliststring = jedisPool.getResource().get("setmeallist");
            objects = null;
            if (setmealliststring!=null){
                objects= JSONObject.parseArray(setmealliststring);
                System.out.println("从redis取");
            }else {
                setmealliststring = getfromsql();
                objects= JSONObject.parseArray(setmealliststring);
                saveToRedis(setmealliststring);
                System.out.println("从数据库查询");

            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("服务器异常,从sql获得");
            setmealliststring = getfromsql();
            objects= JSONObject.parseArray(setmealliststring);
        }


        return objects;
    }

    //把套餐存进redis
    private void saveToRedis(String setmealliststring) {
        //把List转成string存进去redis
        jedisPool.getResource().set("setmeallist", setmealliststring);

    }

    //查询所有套餐
    public String getfromsql() {
        List<Setmeal> list =   setmealDao.findall();
        String liststring = JSONObject.toJSONString(list);
        return liststring;
    }

    /**
     * 套餐详情
     * @param id

     * @return
     */
    @Override
    public JSONObject findById(Integer id) {
        JSONObject jsonObject=null;
        String setmealString=null;
        try {
            setmealString = jedisPool.getResource().get("setmeal"+id);
            if (setmealString!=null){
                 jsonObject = JSONObject.parseObject(setmealString);
                System.out.println("从redis取");
            }else {
                setmealString = getsetmealfromsql(id);
                jsonObject = JSONObject.parseObject(setmealString);
                saveSetmealToRedis(setmealString,id);
                System.out.println("从数据库查询");

            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("服务器异常,从sql获得");
            setmealString = getsetmealfromsql(id);
            jsonObject= JSONObject.parseObject(setmealString);
        }

        return jsonObject;
    }

    //根据ID查询套餐
    public String getsetmealfromsql(Integer id) {
        Setmeal setmeal = setmealDao.findById(id);
        String jsonString = JSONObject.toJSONString(setmeal);
        return jsonString;
    }
    //把套餐数据存到redis
    private void saveSetmealToRedis(String setmealString,Integer id) {
        if (setmealString!=null){
            jedisPool.getResource().set("setmeal"+id, setmealString);
        }
    }
    /**
     * 删除套餐
     * @return
     */
    @Override
    public void delete(Integer id) {
        //查询setmeal表的关联表t_setmeal_checkgroup的count
      long count =  setmealDao.findSetmealSheckgroupByid(id);
        //如果count>0,先删除关联表对应的数据
        if (count>0){
            setmealDao.deleteSetmealSheckgroupByid(id);
        }
        //再删除套餐
        setmealDao.deleteSetmeal(id);
    }


    /**
     * 根据id查询套餐
     * @return
     */
    @Override
    public Setmeal findSetmealByid(Integer id) {
        Setmeal setmeal =   setmealDao.findSetmealByid(id);
        return setmeal;
    }

    /**
     * 根据套餐id查询检查组
     * @return
     */
    @Override
    public List<Integer> findGroupIdsBySetmealid(Integer id) {
        List<Integer>  integerList =setmealDao.findGroupIdsBySetmealid(id);
        return integerList;
    }

    /**
     * 编辑套餐
     * @return
     */
    @Override
    public void editTrue(Setmeal setmeal, Integer[] checkgroupIds) {
            setmealDao.updateSetmeal(setmeal);
        if (checkgroupIds!=null&&checkgroupIds.length>0){

            setmealDao.deleteCheckgroupIdsBySetmealId(setmeal.getId());

            for (Integer checkgroupId : checkgroupIds) {
            Map map = new HashMap();
            map.put("setmealid", setmeal.getId());
            map.put("checkgroupId",checkgroupId);
            setmealDao.insertCheckgroupIdsBySetmealId(map);
            }
        }
    }



    /**
     * 查询套餐数量
     * @return
     */
    @Override
    public List<Map<String, Object>> findSetmealCount() {
        List<Map<String, Object>> setmealCount =  setmealDao.findSetmealCount();
        return setmealCount;
    }



}
